package com.aoxin.service.impl.opm;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.aoxin.mapper.opm.SlfMenuMapper;
import com.aoxin.mapper.opm.SlfRoleMenuMapper;
import com.aoxin.model.dto.MenuDto;
import com.aoxin.model.dto.SelectTreeDto;
import com.aoxin.model.dto.TreeDto;
import com.aoxin.model.opm.SlfMenu;
import com.aoxin.service.inter.opm.MenuService;
import com.aoxin.util.ErrorUtil;

@Service("menuService")
public class MenuServiceImpl implements MenuService {

	@Autowired
	private SlfMenuMapper slfMenuMapper;

	@Autowired
	private SlfRoleMenuMapper slfRoleMenuMapper;

	@Override
	@Transactional
	public void insertSlfMenu(SlfMenu menu) {
		Assert.notNull(menu, String.format(ErrorUtil.PARAMETER_NULL, "menu"));
		menu.setCreateDate(new Date());
		if (null == menu.getSequence()) {
			menu.setSequence(this.slfMenuMapper.getNextSequence(menu.getParentId()).intValue());
		}
		/*if (this.slfMenuMapper.checkName(menu.getName(), null) > 0) {
			throw new IllegalArgumentException(String.format(ErrorUtil.NAME_REPEAT, menu.getName()));
		}*/
		this.slfMenuMapper.insertSelective(menu);
	}

	@Override
	@Transactional
	public void deleteSlfMenu(Integer menuId) {
		Assert.notNull(menuId, String.format(ErrorUtil.PARAMETER_NULL, "menuId"));
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("partId", menuId);
		if (this.slfMenuMapper.getRecordCount(map) > 0) {
			throw new IllegalArgumentException(ErrorUtil.HAS_PARENT_ORG);
		}
		map.clear();
		map.put("menuId", menuId);
		if (this.slfRoleMenuMapper.getRecordCount(map) > 0) {
			throw new IllegalArgumentException(ErrorUtil.MENU_ROLE);
		}
		this.slfMenuMapper.deleteByPrimaryKey(menuId);
	}

	@Override
	@Transactional
	public void deleteSlfMenuByIds(List<Integer> menuIds) {
		Assert.notEmpty(menuIds, String.format(ErrorUtil.PARAMETER_NULL, "menuIds"));
		Map<String, Object> map = new HashMap<String, Object>();
		for (Integer integer : menuIds) {
			map.clear();
			map.put("parentId", integer);
			if (this.slfMenuMapper.getRecordCount(map) > 0) {
				throw new IllegalArgumentException(ErrorUtil.HAS_PARENT_MENU);
			}
			map.clear();
			map.put("menuId", integer);
			if (this.slfRoleMenuMapper.getRecordCount(map) > 0) {
				throw new IllegalArgumentException(ErrorUtil.MENU_ROLE);
			}
		}
		this.slfMenuMapper.deleteByPrimaryKeys(menuIds);
	}

	@Override
	@Transactional
	public void updateSlfMenu(SlfMenu menu) {
		Assert.notNull(menu, String.format(ErrorUtil.PARAMETER_NULL, "menu"));
		/*if (this.slfMenuMapper.checkName(menu.getName(), menu.getMenuId()) > 0) {
			throw new IllegalArgumentException(String.format(ErrorUtil.NAME_REPEAT, menu.getName()));
		}*/
		this.slfMenuMapper.updateByPrimaryKeySelective(menu);
	}

	@Override
	@Transactional
	public void updateSlfMenuStates(List<Integer> menuIds, Integer state) {
		Assert.notEmpty(menuIds, String.format(ErrorUtil.PARAMETER_NULL, "menuIds"));
		Assert.notNull(state, String.format(ErrorUtil.PARAMETER_NULL, "menuId"));
		this.slfMenuMapper.updateStates(menuIds, state);
	}

	@Override
	public SlfMenu getSlfMenu(Integer menuId) {
		Assert.notNull(menuId, String.format(ErrorUtil.PARAMETER_NULL, "menuId"));
		return this.slfMenuMapper.selectByPrimaryKey(menuId);
	}

	@Override
	public List<SlfMenu> getSlfMenuList(Map<String, Object> params) {
		List<SlfMenu> slfMenus=new ArrayList<SlfMenu>();
		if(params.containsKey("parentId")){
			slfMenus = this.slfMenuMapper.getList(params);
		}else{
			params.put("parentId", "0");
			slfMenus = this.slfMenuMapper.getList(params);
		}
		packagingMenu(slfMenus, params);
		return slfMenus;
	}

	/**
	 * 递归查询树
	 * 
	 * @param slfMenus
	 *            机构集合
	 * @param params
	 *            条件集合
	 */
	private void packagingMenu(List<SlfMenu> slfMenus, Map<String, Object> params) {
		if (slfMenus.size() > 0) {
			for (SlfMenu slfMenu : slfMenus) {
				params.put("parentId", slfMenu.getMenuId());
				List<SlfMenu> trees = this.slfMenuMapper.getList(params);
				for(SlfMenu slfMenu1 : trees){
					slfMenu1.setParentName(slfMenu.getName());
				}
				if (trees.size() > 0) {
					slfMenu.setChildren(trees);
				} else {
					slfMenu.setChildren(null);
				}
				packagingMenu(trees, params);
			}
		}
	}

	@Override
	public Long getNextSequence(Integer parentId) {
		return this.slfMenuMapper.getNextSequence(parentId);
	}

	@Override
	public List<MenuDto> getEmployeeMenuList(Integer employeeId) {
		List<MenuDto> menus = this.slfMenuMapper.getEmployeeMenuList(employeeId, 0, null);
		packagingMenuList(menus, employeeId);
		return menus;
	}

	/**
	 * 递归查询菜单树
	 * 
	 * @param menus
	 */
	private void packagingMenuList(List<MenuDto> menus, Integer employeeId) {
		if (menus.size() > 0) {
			for (MenuDto menu : menus) {
				List<MenuDto> list = this.slfMenuMapper.getEmployeeMenuList(employeeId, menu.getParentId(), null);
				if (list.size() > 0) {
					menu.setChildren(list);
				} else {
					menu.setChildren(null);
				}
				packagingMenuList(list, employeeId);
			}
		}
	}

	@Override
	public Map<String, Boolean> getEmployeeMenuButtonList(Integer employeeId, Integer parentId) {
		Assert.notNull(employeeId, String.format(ErrorUtil.PARAMETER_NULL, "employeeId"));
		Assert.notNull(parentId, String.format(ErrorUtil.PARAMETER_NULL, "parentId"));
		List<Map<String, Object>> list = this.slfMenuMapper.getEmployeeMenuButtonList(employeeId, parentId);
		Map<String, Boolean> map = new HashMap<String, Boolean>();
		for (Map<String, Object> listmap : list) {
			map.put(String.valueOf(listmap.get("name")),Boolean.valueOf(String.valueOf(listmap.get("role"))));
		}
		return map;
	}

	@Override
	public List<TreeDto> getMenuTreeList() {
		List<TreeDto> treeDtos = new ArrayList<TreeDto>();
		treeDtos = this.slfMenuMapper.getMenuTreeList(0);
		packagingMenuTree(treeDtos);
		return treeDtos;
	}

	/**
	 * 递归查询菜单树
	 * 
	 * @param treeDtos
	 */
	private void packagingMenuTree(List<TreeDto> treeDtos) {
		if (treeDtos != null && treeDtos.size() > 0) {
			for (TreeDto treeDto : treeDtos) {
				if (null != treeDto) {
					List<TreeDto> trees = this.slfMenuMapper.getMenuTreeList(Integer.parseInt(treeDto.getValue()));
					if (trees.size() > 0) {
						treeDto.setChildren(trees);
					} else {
						treeDto.setChildren(null);
					}

					packagingMenuTree(trees);
				}
			}
		}
	}

	@Override
	public Map<String, Object> getMenuSelectTreeList(Integer roleId) {
		List<SelectTreeDto> treeDtos = new ArrayList<SelectTreeDto>();
		treeDtos = this.slfMenuMapper.getMenuSelectTreeList(0);
		packagingMenuSelectTree(treeDtos);
		List<String> selectMenuIds = this.slfRoleMenuMapper.getMenuListByRoleId(roleId);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("selectTree", treeDtos);
		map.put("selectMenuIds", selectMenuIds);
		return map;
	}

	/**
	 * 递归查询菜单现现选择树
	 * 
	 * @param treeDtos
	 */
	private void packagingMenuSelectTree(List<SelectTreeDto> treeDtos) {
		if (treeDtos.size() > 0) {
			for (SelectTreeDto treeDto : treeDtos) {
				List<SelectTreeDto> trees = this.slfMenuMapper.getMenuSelectTreeList(Integer.parseInt(treeDto.getKey()));
				treeDto.setChildren(trees);
				packagingMenuSelectTree(trees);
			}
		}
	}
}
